package edu.ccut.saturn.studio.dialog.datadialog;

import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.TitleAreaDialog;
import org.eclipse.jface.window.Window;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseTrackAdapter;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

import edu.ccut.saturn.studio.Activator;
import edu.ccut.saturn.studio.Images;
import edu.ccut.saturn.studio.core.search.SaturnFileSearcher.SearchResult;
import edu.ccut.saturn.studio.core.search.dialog.OneCompontentSearchDialog;
import edu.ccut.saturn.studio.core.util.eclipse.Messages;
import edu.ccut.saturn.studio.core.util.eclipse.SaturnValidation;
import edu.ccut.saturn.studio.dialog.SelectionCandidate;
import edu.ccut.saturn.studio.logic.model.saturn.SaturnDataNode;

public class SaturnDataDialog extends TitleAreaDialog {
	
	private Text idText;
	private Text textElementKey;
	private Text textXmlKey;
	private Combo comboScope;
	private Combo comboClassType;
	private Text  textdataKey, textdataPrefix, textDec, textName, 
				 textNameSpace;
	
	private Label imageLabel;
	
	private SaturnDataNode saturnDataNode;
	
	
	/**
	 * Create the dialog
	 * @param parentShell
	 */
	public SaturnDataDialog(Shell parentShell,SaturnDataNode saturnDataNode) {
		super(parentShell);
		this.saturnDataNode = saturnDataNode;
	}
	
	/**
	 * Create contents of the dialog
	 * @param parent
	 */
	@Override
	protected Control createDialogArea(Composite parent) {
		
		setMessage(SelectionCandidate.SETTING_ATTRIBUTE + saturnDataNode.getName().trim()); 
		setTitleImage(Activator.getImage(Images.SATURN_ICONS_MAXNAME));
		
		Composite area = (Composite) super.createDialogArea(parent);
		Composite container = new Composite(area, SWT.NONE);
		final GridLayout gridLayout = new GridLayout();
		gridLayout.numColumns = 3;
		container.setLayout(gridLayout);
		final GridData gd_container = new GridData(SWT.FILL, SWT.FILL, true, true);
		gd_container.heightHint = 297;
		container.setLayoutData(gd_container);

		final Label idLabel = new Label(container, SWT.NONE);
		idLabel.setText("ID：");

		idText = new Text(container, SWT.READ_ONLY | SWT.BORDER);
		final GridData gd_idText = new GridData(SWT.FILL, SWT.CENTER, true, false);
		idText.setLayoutData(gd_idText);
		idText.setText(saturnDataNode.getId().trim());
		new Label(container, SWT.NONE);

		final Label classtypeLabel = new Label(container, SWT.NONE);
		final GridData gd_classtypeLabel = new GridData();
		gd_classtypeLabel.verticalIndent = 5;
		classtypeLabel.setLayoutData(gd_classtypeLabel);
		classtypeLabel.setText(Messages.getString("SaturnDataDialog.CreateDataType"));  //$NON-NLS-1$
		
	comboClassType = new Combo(container, SWT.READ_ONLY);
		final GridData gd_comboClassType = new GridData(SWT.FILL, SWT.CENTER, true, false,2,1);
		gd_comboClassType.verticalIndent = 5;
		comboClassType.setLayoutData(gd_comboClassType);
		
		String iniClassType []  = new String[]{"edu.ccut.saturn.component.SaturnData","list","java.util.Map"};
		comboClassType.setItems(iniClassType);
		if("edu.ccut.saturn.component.SaturnData".equals(saturnDataNode.getClasstype().trim())){
			comboClassType.select(0);
		}else if("java.util.Map".equals(saturnDataNode.getClasstype())){
			comboClassType.select(2);
		}else{
			comboClassType.select(1);
		}
		
		final Label datekeyLabel = new Label(container, SWT.NONE);
		final GridData gd_datekeyLabel = new GridData();
		gd_datekeyLabel.verticalIndent = 5;
		datekeyLabel.setLayoutData(gd_datekeyLabel);
		datekeyLabel.setText(Messages.getString("SaturnDataDialog.CreateDataKeyValue"));  //$NON-NLS-1$
		
		textdataKey = new Text(container, SWT.BORDER);
		textdataKey.setText(saturnDataNode.getDatakey().trim());
		textdataKey.addModifyListener(new ModifyListener() {
			public void modifyText(final ModifyEvent e) {
				validate();
			}
		});
		textdataKey.setTextLimit(100);
		final GridData gd_textdataKey = new GridData(SWT.FILL, SWT.CENTER, true, false,2,1);
		gd_textdataKey.verticalIndent = 5;
		textdataKey.setLayoutData(gd_textdataKey);
		

		final Label dateprixLabel = new Label(container, SWT.NONE);
		final GridData gd_dateprixLabel = new GridData();
		gd_dateprixLabel.verticalIndent = 5;
		dateprixLabel.setLayoutData(gd_dateprixLabel);
		dateprixLabel.setText(Messages.getString("SaturnDataDialog.AcceptDataPrefix"));   //$NON-NLS-1$
		
		textdataPrefix = new Text(container, SWT.BORDER);
		textdataPrefix.setTextLimit(100);
		final GridData gd_textdataPrefix = new GridData(SWT.FILL, SWT.CENTER, true, false,2,1);
		gd_textdataPrefix.verticalIndent = 5;
		textdataPrefix.setLayoutData(gd_textdataPrefix);
		textdataPrefix.setText(saturnDataNode.getPrefix().trim());

		final Label datascopeLabel = new Label(container, SWT.NONE);
		final GridData gd_datascopeLabel = new GridData();
		gd_datascopeLabel.verticalIndent = 5;
		datascopeLabel.setLayoutData(gd_datascopeLabel);
		datascopeLabel.setText(Messages.getString("SaturnDataDialog.AcceptDataType"));  //$NON-NLS-1$
		
		comboScope = new Combo(container, SWT.READ_ONLY);
		final GridData gd_comboScope = new GridData(SWT.FILL, SWT.CENTER, true, false,2,1);
		comboScope.setLayoutData(gd_comboScope);
		comboScope.setItems(new String[] {"request", "xml", "json"});
		comboScope.setText(saturnDataNode.getScope().trim());
		
		final Label decLabel = new Label(container, SWT.NONE);
		final GridData gd_decLabel = new GridData();
		gd_decLabel.verticalIndent = 5;
		decLabel.setLayoutData(gd_decLabel);
		decLabel.setText(Messages.getString("SaturnDataDialog.description"));   //$NON-NLS-1$
		
		textDec = new Text(container, SWT.BORDER);
		textDec.setTextLimit(200);
		final GridData gd_textDec = new GridData(SWT.FILL, SWT.CENTER, true, false,2,1);
		gd_textDec.verticalIndent = 5;
		textDec.setLayoutData(gd_textDec);
		textDec.setText(saturnDataNode.getDescription().trim());

		final Label nameLabel = new Label(container, SWT.NONE);
		final GridData gd_nameLabel = new GridData();
		gd_nameLabel.verticalIndent = 5;
		nameLabel.setLayoutData(gd_nameLabel);
		nameLabel.setText(Messages.getString("SaturnDataDialog.name"));   //$NON-NLS-1$
		
		textName = new Text(container, SWT.BORDER);
		textName.setTextLimit(50);
		final GridData gd_textName = new GridData(SWT.FILL, SWT.CENTER, true, false,2,1);
		gd_textName.verticalIndent = 5;
		textName.setLayoutData(gd_textName);
		textName.setText(saturnDataNode.getName().trim());

		final Label namespaceLabel = new Label(container, SWT.NONE);
		final GridData gd_namespaceLabel = new GridData();
		gd_namespaceLabel.verticalIndent = 5;
		namespaceLabel.setLayoutData(gd_namespaceLabel);
		namespaceLabel.setText(Messages.getString("SaturnDataDialog.DataKeyValue"));   //$NON-NLS-1$
		
		textNameSpace = new Text(container, SWT.READ_ONLY | SWT.BORDER);

		textNameSpace.setTextLimit(100);
		final GridData gd_textNameSpace = new GridData(SWT.FILL, SWT.CENTER, true, false);
		gd_textNameSpace.verticalIndent = 5;
		textNameSpace.setLayoutData(gd_textNameSpace);
		if(saturnDataNode.getNamespace().trim().length()!=0&&saturnDataNode.getNamespace().trim().length()!=0){
			textNameSpace.setText(saturnDataNode.getNamespace().trim()+"::"+saturnDataNode.getSaturndatatype().trim());
		}
		textNameSpace.addModifyListener(new ModifyListener() {
			public void modifyText(final ModifyEvent e) {
				validate();
			}
		});
		
		imageLabel = new Label(container, SWT.NONE);
		imageLabel.addMouseListener(new MouseAdapter() {
			public void mouseDown(final MouseEvent e) {
//				SearchSaturnDataKeyDialog dialog1 = new SearchSaturnDataKeyDialog(Display.getDefault().getShells()[0], saturnDataNode);
//				
//				dialog1.setTitle(Messages.getString("SaturnDataDialog.SelectSaturnData")); //$NON-NLS-1$
//				
//				if (dialog1.open() == Window.OK) {
//					String dataItemkey = (String)dialog1.getFirstResult();
//					textNameSpace.setText(dataItemkey);
					OneCompontentSearchDialog searchDialog = new OneCompontentSearchDialog(
							Display.getDefault().getShells()[0],saturnDataNode);
					
					if (searchDialog.open() == Window.OK) {
						SearchResult dataItemKey = (SearchResult)searchDialog.getFirstResult();
						textNameSpace.setText(dataItemKey.getKey());
					}
				}
			}
		);
		imageLabel.addMouseTrackListener(new MouseTrackAdapter() {
			public void mouseEnter(final MouseEvent e) {
				imageLabel.setImage(Activator.getImage(Images.AMPLIFY_MINUS));
			}
			public void mouseExit(final MouseEvent e) {
				imageLabel.setImage(Activator.getImage(Images.AMPLIFY_PLUS));
			}
		});
		final GridData gd_imageLabel = new GridData();
		gd_imageLabel.verticalIndent = 5;
		imageLabel.setLayoutData(gd_imageLabel);
		imageLabel.setImage(Activator.getImage(Images.AMPLIFY_PLUS));

		final Label xmlkeyLabel = new Label(container, SWT.NONE);
		final GridData gd_xmlkeyLabel = new GridData();
		gd_xmlkeyLabel.verticalIndent = 5;
		xmlkeyLabel.setLayoutData(gd_xmlkeyLabel);
		xmlkeyLabel.setText("XmlKey:");

		textXmlKey = new Text(container, SWT.BORDER);
		textXmlKey.setText(saturnDataNode.getXmlKey());
		textXmlKey.addModifyListener(new ModifyListener() {
			public void modifyText(final ModifyEvent e) {
				validate();
			}
		});
		textXmlKey.setTextLimit(100);
		final GridData gd_textXmlKey = new GridData(SWT.FILL, SWT.CENTER, true, false,2,1);
		gd_textXmlKey.verticalIndent = 5;
		textXmlKey.setLayoutData(gd_textXmlKey);
		

		final Label elementkeyLabel = new Label(container, SWT.NONE);
		final GridData gd_elementkeyLabel = new GridData();
		gd_elementkeyLabel.verticalIndent = 5;
		elementkeyLabel.setLayoutData(gd_elementkeyLabel);
		elementkeyLabel.setText("ElementKey:");

		textElementKey = new Text(container, SWT.BORDER);
		textElementKey.setText(saturnDataNode.getElementKey());
		textElementKey.addModifyListener(new ModifyListener() {
			public void modifyText(final ModifyEvent e) {
				validate();
			}
		});
		textElementKey.setTextLimit(100);
		final GridData gd_textElementKey = new GridData(SWT.FILL, SWT.CENTER, true, false,2,1);
		gd_textElementKey.verticalIndent = 5;
		textElementKey.setLayoutData(gd_textElementKey);
		
		
		comboScope.addModifyListener(new ModifyListener() {
			public void modifyText(final ModifyEvent e) {
				validate();
				if(comboScope.getText().trim().equals("xml")){
					textElementKey.setEnabled(true);
					textXmlKey.setEnabled(true);
				}else if(comboScope.getText().trim().equals("json")) {
					textElementKey.setEnabled(false);
					textXmlKey.setEnabled(true);
				} else{
					textElementKey.setEnabled(false);
					textXmlKey.setEnabled(false);
				}
			}
		});
		if(comboScope.getText().trim().equals("json")){
			textElementKey.setEnabled(false);
			textXmlKey.setEnabled(true);
		} else if(comboScope.getText().trim().equals("xml")){
			textElementKey.setEnabled(true);
			textXmlKey.setEnabled(true);
		} else {
			textElementKey.setEnabled(false);
			textXmlKey.setEnabled(false);
		}

		/**
		 * 名称不可为空
		 * 
		 * */
		textName.addModifyListener(new ModifyListener() {
			public void modifyText(final ModifyEvent e) {
				
				
				validate();
				
			}
		});
		
		return area;
	}
	
	private boolean  validate(){
		
//		getButton(IDialogConstants.OK_ID).setEnabled(false);
		
		if(SaturnValidation.isEmpty(textName.getText().trim())){
			setErrorMessage(Messages.getString("SaturnDataDialog.NameLimit"));   //$NON-NLS-1$
			getButton(IDialogConstants.OK_ID).setEnabled(false);
		    return false;
		} else if (SaturnValidation.isEmpty(textdataKey.getText())) {
			setErrorMessage(Messages.getString("SaturnDataDialog.datakeyIsNotNull"));   //$NON-NLS-1$
			getButton(IDialogConstants.OK_ID).setEnabled(false);
			return false;
		} else if (comboScope.getText().endsWith("xml")) {
			if (SaturnValidation.isEmpty(textXmlKey.getText())) {
				setErrorMessage(Messages.getString("SaturnDataDialog.XmlKeyIsNotNull"));   //$NON-NLS-1$
				getButton(IDialogConstants.OK_ID).setEnabled(false);
				return false;
			} 
			if (SaturnValidation.isEmpty(textElementKey.getText())) {
				getButton(IDialogConstants.OK_ID).setEnabled(false);
				setErrorMessage(Messages.getString("SaturnDataDialog.ElementKeyIsNotNull"));    //$NON-NLS-1$
				return false;
			}
		} else if (SaturnValidation.isEmpty(textNameSpace.getText())) {
			getButton(IDialogConstants.OK_ID).setEnabled(false);
			setErrorMessage(Messages.getString("SaturnDataDialog.dataKeyIsNotNull"));   //$NON-NLS-1$
			return false;
				
		} else {
			setErrorMessage(null);
			getButton(IDialogConstants.OK_ID).setEnabled(true);
			return true;
		}
		setErrorMessage(null);
		getButton(IDialogConstants.OK_ID).setEnabled(true);
		return true;
		
	}
	
	
	/**
	 * Return the initial size of the dialog
	 */
	@Override
	protected Point getInitialSize() {
		return new Point(500, 421);
	}
	
	protected void configureShell(Shell newShell) {
		super.configureShell(newShell);
		newShell.setText(SelectionCandidate.ATTRIBUTE_SETTING);
		newShell.setImage(Activator.getImage(Images.SATURN_ICONS_SMALLNAME));
		
	}
	
	protected int getShellStyle() {
		return SWT.CLOSE | SWT.RESIZE;
	}
	
	protected void buttonPressed(int buttonId) {
		if (buttonId == IDialogConstants.OK_ID) {
			saturnDataNode.setClasstype(comboClassType.getText().trim());
			saturnDataNode.setDatakey(textdataKey.getText().trim());
			saturnDataNode.setPrefix(textdataPrefix.getText().trim());
			if (textNameSpace.getText()!=null&&textNameSpace.getText().length()!=0) {
				saturnDataNode
						.setNamespace(textNameSpace.getText().split("::")[0]);
				saturnDataNode.setSaturndatatype(textNameSpace.getText().split("::")[1]);
			}
//			saturnDataNode.setSaturndatatype(textXsdTypeName.getText().trim());
			saturnDataNode.setDescription(textDec.getText().trim());
			if("logic".equals(comboScope.getText())){ 
				saturnDataNode.setScope("1"); 
			}
			else if("request".equals(comboScope.getText().trim())){ 
				saturnDataNode.setScope("2"); 
			}
			else if("session".equals(comboScope.getText().trim())){ 
				saturnDataNode.setScope("5"); 
			}
			else if("application".equals(comboScope.getText().trim())){ 
				saturnDataNode.setScope("10"); 
			} 
			else if("json".equals(comboScope.getText().trim())) {
				saturnDataNode.setScope("json");
				saturnDataNode.setXmlKey(textXmlKey.getText().trim());
			} 
			else{
				saturnDataNode.setScope("xml");
				((SaturnDataNode) saturnDataNode).setXmlKey(textXmlKey.getText().trim());
				saturnDataNode.setElementKey(textElementKey.getText().trim());
			}
			
			saturnDataNode.setName(textName.getText().trim());
		}
		super.buttonPressed(buttonId);
	}
	
	@Override
	protected void createButtonsForButtonBar(Composite parent) {
		super.createButtonsForButtonBar(parent);
		getButton(IDialogConstants.OK_ID).setEnabled(false);
	}
}
